package com.tiaocai.module;

import com.tiaocai.bean.User;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.adaptor.JsonAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;

import java.util.List;
import java.util.Map;

@IocBean
@At("/user")
@Fail("http:500")
public class UserModule {

    @Inject
    protected Dao dao;

    @At
    @Ok("jsp:jsp/user/uindex")
    public void index(){

    }

    @At
    @Ok("json")
    public int count() {
        return dao.count(User.class);
    }

    @At
    @Ok("json")
    public List<User> list(){

        return dao.query(User.class, Cnd.where("1","=","1"));
    }


    @At
    @Ok("json")
    @AdaptBy(type = JsonAdaptor.class)  // http://nutzam.com/core/mvc/http_adaptor.html
    public Map create(User user){

        //写数据到数据库，需要添加一些逻辑判断写是否成功
        dao.insert(user);
        return new NutMap().addv("code",0).addv("msg","ok");

    }

}
